<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="关键字">
              <a-input placeholder="请输入统一信用代码、简称、投资机构等" v-model="queryParam.key"></a-input>
            </a-form-item>
          </a-col>
        
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-button type="primary" icon="download" @click="showDownloadSet = true">导出设置</a-button>
      <a-button type="primary" icon="import" @click="handleImportExcelPy">服务拉取</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('融资历程-一周', 7)">导出一周数据</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('融资历程-一月', 30)">导出一月数据</a-button> -->
<!--      <a-dropdown v-if="selectedRowKeys.length > 0">-->
<!--        <a-menu slot="overlay">-->
<!--          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>-->
<!--        </a-menu>-->
<!--        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>-->
<!--      </a-dropdown>-->
    </div>
    <div v-if="showDownloadSet">
      <div class="configRow clearfix">
        <label>省份地区：</label>
        <div class="row-data">
          <a-tag :color="!exportParam.provinceId ? 'blue': null" @click="setProvince('')" class="cursor-pointer">
            不限
          </a-tag>
          <a-tag class="cursor-pointer" :color="exportParam.provinceId === item.RegionId ? 'blue': null" v-for="(item,index) in provinceData" @click="setProvince(item.RegionId)" :key="index">
            {{item.RegionName}}
          </a-tag>
        </div>
      </div>
      <div class="configRow clearfix">
        <label>城市：</label>
        <div class="row-data">
          <a-tag :color="!exportParam.cityId ? 'blue': null" @click="setProvince('')" class="cursor-pointer">
            不限
          </a-tag>
          <a-tag class="cursor-pointer" :color="exportParam.cityId === item.RegionId ? 'blue': null" v-for="(item,index) in cityData" @click="setCity(item.RegionId)" :key="index">
            {{item.RegionName}}
          </a-tag>
        </div>
      </div>
      <div class="configRow clearfix">
        <label>地区：</label>
        <div class="row-data">
          <a-tag :color="!exportParam.districtId ? 'blue': null" @click="setProvince('')" class="cursor-pointer">
            不限
          </a-tag>
          <a-tag class="cursor-pointer" :color="exportParam.districtId === item.RegionId ? 'blue': null" v-for="(item,index) in districtData" @click="setDistrict(item.RegionId)" :key="index">
            {{item.RegionName}}
          </a-tag>
        </div>
      </div>
      <div class="configRow clearfix">
        <label>融资日期：</label>
        <div class="row-data">
          <a-tag :color="!exportParam.timeLength ? 'blue': null" @click="setTimeLength('')" class="cursor-pointer">
            不限
          </a-tag>
          <a-tag class="cursor-pointer" :color="exportParam.timeLength === item.RegionId ? 'blue': null" v-for="(item,index) in refinanceDateData" @click="setTimeLength(item.RegionId)" :key="index">
            {{item.RegionName}}
          </a-tag>
          <a-range-picker
            v-model="exportParam.selfTime"
            @change="setSelfTime"
            :show-time="{
              hideDisabledOptions: true,
              defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')],
            }"
            format="YYYY-MM-DD HH:mm:ss"
          />
        </div>
      </div>
      <div class="configRow clearfix">
        <label>一级行业：</label>
        <div class="row-data">
          <a-tag :color="!exportParam.primaryIndustryId ? 'blue': null" @click="setPrimaryIndustry('')" class="cursor-pointer">
            不限
          </a-tag>
          <a-tag class="cursor-pointer" :color="exportParam.primaryIndustryId === item.BaseIndustryId ? 'blue': null" v-for="(item,index) in industryData" @click="setPrimaryIndustry(item.BaseIndustryId)" :key="index">
            {{item.Industry}}
          </a-tag>
        </div>
      </div>
      <div class="configRow clearfix">
        <label>二级行业：</label>
        <div class="row-data">
          <a-tag :color="!exportParam.subIndustryId ? 'blue': null" @click="setSubIndustry('')" class="cursor-pointer">
            不限
          </a-tag>
          <a-tag class="cursor-pointer" :color="exportParam.subIndustryId === item.BaseIndustryId ? 'blue': null" v-for="(item,index) in secIndustryData" @click="setSubIndustry(item.BaseIndustryId)" :key="index">
            {{item.Industry}}
          </a-tag>
        </div>
      </div>
      <div class="configRow clearfix">
        <label>三级行业：</label>
        <div class="row-data">
          <a-tag :color="!exportParam.thirdIndustryId ? 'blue': null" @click="setThirdIndustry('')" class="cursor-pointer">
            不限
          </a-tag>
          <a-tag class="cursor-pointer" :color="exportParam.thirdIndustryId === item.BaseIndustryId ? 'blue': null" v-for="(item,index) in thirdIndustryData" @click="setThirdIndustry(item.BaseIndustryId)" :key="index">
            {{item.Industry}}
          </a-tag>
        </div>
      </div>
      <div class="configRow clearfix">
        <label>融资轮次：</label>
        <div class="row-data">
          <a-checkbox-group @change="setRound">
            <a-row>
              <a-col :span="2" v-for="(item,index) in refinanceRoundData" :key="index">
                <a-checkbox :value="item.BaseInfoId">
                  {{item.Value}}
                </a-checkbox>
              </a-col>
            </a-row>
          </a-checkbox-group>
          <!-- <a-tag :color="!exportParam.refinanceRoundId ? 'blue': null" @click="setRound(null)" class="cursor-pointer">
            不限
          </a-tag>
          <a-tag class="cursor-pointer" :color="exportParam.refinanceRoundId === item.BaseInfoId ? 'blue': null" v-for="(item,index) in refinanceRoundData" @click="setRound(item.BaseInfoId)" :key="index">
            {{item.Value}}
          </a-tag> -->
        </div>
      </div>
      <div class="configRow clearfix" style="text-align:right">
        <a-button @click="showDownloadSet = false" style="margin-right:10px">收起</a-button>
        <a-button type="primary" @click="handleExportXlsSelf('融资历程', exportParam)">确定</a-button>
      </div>
    </div>
    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;height:40px;">
        <setColumnFields @chooseFields="chooseFields" :columns="columnsCopy"></setColumnFields>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">
        <span slot="tag" slot-scope="text, record">
          <template v-if="text">
            <span class="tagSpan" style="margin-top:5px" v-for="(item,index) in text.split(',')">{{item}}</span>
          </template>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDeleteById(record, 'CompanyRefinanceId')">
            <a>删除</a>
          </a-popconfirm>
          <!-- 
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown> -->
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <companyRefinance-modal ref="modalForm" @ok="modalFormOk"></companyRefinance-modal>
    <a-modal
      :title="'拉取信息'"
      :width="800"
      :visible="showModal"
      switchFullscreen
      @cancel="showModal = false"
      :class="'pic-preview'"
      cancelText="关闭">
      {{ modalData }}
    </a-modal>
  </a-card>
</template>

<script>
  import '@/assets/less/TableExpand.less'
  import CompanyRefinanceModal from './modules/CompanyRefinanceModal'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import setColumnFields from '@/components/setColumnsFields'
  import moment from 'moment';
  import axios from "axios"
  export default {
    name: "CompanyRefinanceList",
    mixins:[JeecgListMixin],
    components: {
        CompanyRefinanceModal,setColumnFields
    },
    data () {
      return {
        description: '融资历程表页面',
        showDownloadSet: false,
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
           },
		   // {
     //        title: '统一信用代码',
     //        align:"center",
     //        dataIndex: 'Company_CompanyCode'
     //       },
       {
            title: '公司简称',
            align:"center",
            dataIndex: 'Company_CompanyAbbr',
            isKey:true
           },
           {
            title: '公司代码',
            align:"center",
            dataIndex: 'Company_CompanyCode',
            isKey:true
           },
           {
            title: '公司全称',
            align:"center",
            dataIndex: 'Company_CompanyName',
            isKey:true
           },
           {
            title: '一级行业',
            align:"center",
            dataIndex: 'Company_PrimaryIndustry_Industry'
           },
           {
            title: '二级行业',
            align:"center",
            dataIndex: 'Company_Industry_Industry'
           },
           {
            title: '三级行业',
            align:"center",
            dataIndex: 'Company_SubIndustry',
           },
           
           {
            title: '融资日期',
            align:"center",
            dataIndex: 'RefinanceDate',
            customRender:function (t,r,index) {
              return t?t.split(' ')[0]:'';
            }
           },
           {
            title: '融资轮次',
            align:"center",
            dataIndex: 'RefinanceRound_Value'
           },
           {
            title: '融资金额',
            align:"center",
            dataIndex: 'RefinanceAmt'
           },
           {
            title: '真实融资',
            align:"center",
            dataIndex: 'RefinanceTrueAmt',
            customRender:function (t,r,index) {
              return t?t+'万'+r.CurrencyType_Value:'-';
            }
           },
       {
            title: '投后估值（亿）',
            align:"center",
            dataIndex: 'ValuationAmt',
            customRender:function (t,r,index) {
              return t?t:'-';
            }
           },
		   {
            title: '融资币种',
            align:"center",
            dataIndex: 'CurrencyType_Value'
           },
		   
		   
		   
		      {
            title: '投资方',
            align:"center",
            dataIndex: 'Investors',
            width: 250,
            scopedSlots: { customRender: 'tag' },
           },
           {
            title: '已对外公布',
            align:"center",
            dataIndex: 'IsDisclose',
            customRender:function (t,r,index) {
              return t == 1 ? '是' : t == 0 ? '否' : '';
            }
           },
           {
            title: '有股东变更',
            align:"center",
            dataIndex: 'IsShareholderChanged',
            customRender:function (t,r,index) {
              return t == 1 ? '是' : t == 0 ? '否' : '';
            }
           },
           {
            title: '有注册资本变更',
            align:"center",
            dataIndex: 'IsRegisteredAmtChanged',
            customRender:function (t,r,index) {
              return t == 1 ? '是' : t == 0 ? '否' : '';
            }
           },
		   // {
     //        title: '最近更新人',
     //        align:"center",
     //        dataIndex: 'updateby'
     //       },
		   // {
     //        title: '最近更新日期',
     //        align:"center",
     //        dataIndex: 'Ts'
     //       },
		      {
            title: '最近更新人',
            align:"center",
            dataIndex: 'UpdateBy_Name',
            isKey:true
           },
          {
            title: '最近更新日期',
            align:"center",
            dataIndex: 'Ts',
            customRender:function (t,r,index) {
              return t?new Date(parseInt(t)).toLocaleDateString().replace('/','-').replace('/','-'):'';
            }
           },
          {
            title: '操作',
            dataIndex: '',
            align:"center",
            scopedSlots: { customRender: 'action' },
          }
        ],
        exportParam: {
          provinceId: '',
          cityId: '',
          districtId: '',
          timeLength: '',
          selfTime: null,
          refinanceRoundId: null,
          primaryIndustryId: '',
          subIndustryId: '',
          thirdIndustryId: ''
        },
        provinceData: [],
        cityData: [],
        districtData: [],
        refinanceDateData:[{
          RegionId: 1,
          RegionName: "近1天"
        },{
          RegionId: 7,
          RegionName: "近7天"
        },{
          RegionId: 30,
          RegionName: "近1月"
        },{
          RegionId: 180,
          RegionName: "近半年"
        },{
          RegionId: 360,
          RegionName: "近1年"
        }],
        refinanceRoundData:[],
        industryData:[],
        secIndustryData: [],
        thirdIndustryData: [],
        modalData: null,
        showModal: false,
		url: {
          list: "/companyRefinance/queryRows",
          delete: "/companyRefinance/delete",
          deleteBatch: "/robot/companyRefinance/deleteBatch",
          exportXlsUrl: "robot/companyRefinance/exportXls",
          importExcelUrl: "companyRefinance/import/excel",
          exportXlsUrl: '/companyRefinance/queryRows/excel',
          baseList:"/baseInfo/queryRows",
       },
    }
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
    importExcelUrlPy: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  mounted() {
    this.getProvince()
    this.getRound()
    this.getIndustry()
  },
    methods: {
      handleImportExcelPy(){
        this.$message.success('正在获取数据，请稍等');
        axios.get('http://localhost:8901/ai/company/getRefinaceExcel?fileName=url.xlsx').then(res=> {
          if(res && res.data && res.data.message){
            this.modalData = res.data.message
            this.showModal = true
          } else {
             this.$message.success('拉取失败，请联系管理员');
          }
        }).catch(res=>{
          this.$message.success('本机未安装服务');
        })
      },
      moment,
      getIndustry(){
        this.$http.postAction('/baseIndustry/queryRows',{pageNo:1,pageSize:9999,selectFields:[],conditions:[{
              "field":"UpIndustry.Industry",
              "operator":"IS NULL",
              "value":""
            }]}).then((res)=>{
          if(res.status == 1){
            this.industryData = res.data.rows;
          }
        })
      },
      setPrimaryIndustry(id){
        this.exportParam.primaryIndustryId = id
        this.getErjiIndustry()
      },
      getErjiIndustry(){
        this.exportParam.subIndustryId = ''
        this.exportParam.thirdIndustryId = ''
        this.secIndustryData = []
        this.thirdIndustryData = []
        if (!this.exportParam.primaryIndustryId) {
          return
        }
        this.$http.postAction('/baseIndustry/queryRows',{pageNo:1,pageSize:9999,selectFields:[],conditions:[{
            "field":"UpIndustry",
            "operator":"=",
            "value":this.exportParam.primaryIndustryId
          }]}).then((res)=>{
          if(res.status == 1){
            this.secIndustryData = res.data.rows;
          }
        })
      },
      setSubIndustry(id){
        this.exportParam.subIndustryId = id
        this.getThirdIndustry()
      },
      getThirdIndustry(){
        if (!this.exportParam.subIndustryId) {
          this.exportParam.thirdIndustryId = ''
          this.thirdIndustryData = []
          return
        }
        this.$http.postAction('/baseIndustry/queryRows',{pageNo:1,pageSize:9999,selectFields:[],conditions:[{
            "field":"UpIndustry",
            "operator":"=",
            "value":this.exportParam.subIndustryId
          }]}).then((res)=>{
          if(res.status == 1){
            this.thirdIndustryData = res.data.rows;
          }
        })
      },
      setThirdIndustry(id){
        this.exportParam.thirdIndustryId = id
      },
      getRound() {
        this.$http.postAction(this.url.baseList,{pageIndex:1,pageSize:100,selectFields:[],orders:[{field:'OrderNum',desc:false}],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'RefinanceRound'
            }]}).then((res)=>{
          if(res.status == 1){
            this.refinanceRoundData = res.data.rows;
          }
        })
      },
      setRound(id){
        this.exportParam.refinanceRoundId = id
      },
      getProvince(){
        this.$http.postAction("/baseRegion/queryRows",{pageIndex:1,pageSize:9999,selectFields:[],conditions:[{
              "field":"RegionType",
              "operator":"=",
              "value":1
            }]}).then(res=>{
          if(res.status == 1){
            this.provinceData = res.data.rows;
          }
        })
      },
      setProvince(id){
        this.exportParam.provinceId = id
        this.getCity()
      },
      setCity(id){
        this.exportParam.cityId = id
        this.getDistrict()
      },
      setDistrict(id){
        this.exportParam.districtId = id
      },
      setTimeLength(id){
        this.exportParam.selfTime = null
        this.exportParam.timeLength = id
      },
      setSelfTime(val){
        this.exportParam.selfTime = val
        this.exportParam.timeLength = ''
      },
      getCity(){
        this.exportParam.districtId = ''
        this.districtData = []
        this.exportParam.cityId = ''
        this.cityData = []
        if (!this.exportParam.provinceId) { 
          return
        }
        this.$http.postAction("/baseRegion/queryRows",{pageIndex:1,pageSize:9999,selectFields:[],conditions:[{
            "field":"Parent",
            "operator":"=",
            "value":this.exportParam.provinceId
          }]}).then(res=>{
          if(res.status == 1){
            this.cityData = res.data.rows;
          }
        })
      },
      getDistrict() {
        if (!this.exportParam.cityId) {
          this.exportParam.districtId = ''
          this.districtData = []
          return
        }
        this.$http.postAction("/baseRegion/queryRows",{pageIndex:1,pageSize:9999,selectFields:[],conditions:[{
            "field":"Parent",
            "operator":"=",
            "value":this.exportParam.cityId
          }]}).then(res=>{
          if(res.status == 1){
            this.districtData = res.data.rows;
          }
        })
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
  .configRow{
    /*height:500px;*/
    margin-bottom:10px;
  }
  .configRow >label{
    width: 80px;
    float: left;
    line-height: 30px;
  }
  .configRow .row-data{
    float: left;
    width: calc(100% - 80px);
    line-height: 30px;
  }
</style>